<?php

namespace App\Service\Exam;

use App\Dao\Exam\ExamDao;
use App\Service\BaseService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Str;
use Yj\Apidog\Annotation\PostApi;

class ExamJoinService extends BaseService
{
    /**
     * @Inject()
     * @var ExamDao
     */
    protected $examDao;

    public function lists(){
        $data = $this->examDao->orderByDesc('id')->getList();
        _SUCCESS($data);
    }

    public function question(int $exam_id)
    {
        $data = $this->examDao->DaoWhere(compact('exam_id'))->with(['Question'=>function($query){
            $query->with('QuestionDetail');
        }])->firstOr(function (){
           _Error('不存在该考试');
        });

        $data->Question->transform(function ($question){
            $question->QuestionDetail->transform(function ($questionDetail,$index){
                $questionDetail->option = Str::upper(chr($index+97));
                return $questionDetail;
             });
            return $question;
        });




        return $data;
    }

    public function edit($id,$data)
    {
        $examDao = $this->examDao->DaoWhere(compact('id'))->firstOr(function(){
           _Error('不存在该考试');
        });

        $examDao->fill($data->toArray());
        $examDao->save();
        return true;
    }

    public function del(int $id)
    {
        $examDao = $this->examDao->DaoWhere(compact('id'))->firstOr(function(){
            _Error('不存在该考试');
        });
        $examDao->delete();
        return true;
    }

    public function add(\App\Tool\Collect $data)
    {
        $this->examDao->create($data->toArray());
        return true;
    }

    public function join(\App\Tool\Collect $data)
    {
        
    }
}